# generate a realization of a point pattern model by simulation
# 
# model: model names, e.g. LGCP
# params: parameters for specific model
# covrs:  covariables might be used in simulation
#
# Author: Guochun Shen
# Data:   2011-12-18
# Project:spatial statistic
# Email:  shenguochun@gmail.com
###############################################################################

#TODO test this function
realization=function(model,params,covrs=NULL){
	
	if(!is.null(covrs)){
		np=names(params)
		sel=match(np,names(covrs))
		beta=params[which(!is.na(sel))]
		sel=!is.na(sel)
		lambda=covrs[[1]]
		lambda$v=params[1]
	    for(i in 1:length(beta)){
			lambda$v=lambda$v+covrs[[sel[i]]]$v*beta[i]
		}
		lambda$v=exp(lambda$v)
	}
	
	if(model=="LGCP"){
		sigma2=params[match("sigma2",names(params))]
		alpha=params[match("alpha",names(params))]
		N=params[match("N",names(params))]
		param=c(0,sigma2,0,alpha,NULL)
		e.rp=rLGCP(lambda,sigma2,alpha,N,plotdim=c(covrs[[1]]$xrange[2],covrs[[1]]$yrange[2]))
	}
	return(e.rp)
}

